//由于Map的原因，当前Hash表中的值排在最后面的是最近使用的
class LRU{
    constructor(maxLength = 5){
        this.maxLength = maxLength
        this.hashMap = new Map();
    }
    get(id){
        if(this.hashMap.has(id)){
           console.log("命中");
            let detail = this.hashMap.get(id);
            this.hashMap.delete(id)
            this.hashMap.set(id,detail);
        }
        else{
            return null;
        }
    }
    set(id,key){
        if(this.hashMap.size < this.maxLength){
            this.hashMap.set(id,key)
        }
        else{
            console.log("发生LRU替换");
            let key  = this.hashMap.keys().next().value;
            this.hashMap.delete(key);
            this.hashMap.set(id,key);
        }
    }
    reset(){
        console.log("刷新重置");
        this.hashMap.clear()
    }
}

export{
    LRU
}